package com.ht.controller;



import cn.hutool.json.JSONUtil;
import com.ht.data.Result;
import com.ht.dto.MedicationDiary;
import com.ht.dto.User;
import com.ht.service.IMedicationDiaryService;
import com.ht.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 代齐园
 * @since 2022-05-28
 */
@RestController
@RequestMapping("/medicationDiary")
@Api(value = "用药日记")
public class MedicationDiaryController {
    @Autowired
    private IMedicationDiaryService medicationDiaryService;

    @PostMapping("/insertDiary")
    //value为这个方法干什么用的
    @ApiOperation(value = "用药日记")
    //name的意思是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户token",required = true,paramType = "header"),
            @ApiImplicitParam(name = "medicationDiary",value = "用户用药日记增加的数据",required = true,paramType = "body")
    })
    public Result insertDiary(@RequestHeader("AuthToken")String token, @RequestBody MedicationDiary medicationDiary){
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);
        medicationDiaryService.insertDiary(user.getId(),medicationDiary);
        return Result.OK().setData("添加成功");
    }


    @GetMapping("/getMyDiary")
    @ApiOperation(value = "查询和用户相关的用药日记")
    @ApiImplicitParam(name = "token",value = "用户token",required = true,paramType = "header")
    public Result getMyDiary(@RequestHeader("AuthToken")String token){
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);
        List<MedicationDiary> doctorList = medicationDiaryService.getMyDiary(user.getId());
        return Result.OK().setData(doctorList);
    }
}
